@using FlickrHelpers;
@using System.Collections;
@using DotNetNuke.Entities.Modules;

@{
    int tabModuleId = Dnn.Module.TabModuleID;
    ModuleController moduleController = new ModuleController();
    Hashtable settings = moduleController.GetTabModuleSettings(tabModuleId);

    // Let's set default settings
    if(settings["ImageCount"] == null)
    {
        moduleController.UpdateTabModuleSetting(tabModuleId, "ImageCount", "10");
    }

    if (settings["Display"] == null)
    {
        moduleController.UpdateTabModuleSetting(tabModuleId, "Display", Flickr.DisplayType.random.ToString());
    }

    if (settings["Source"] == null)
    {
        moduleController.UpdateTabModuleSetting(tabModuleId, "Source", Flickr.SourceType.all.ToString());
    }
    ModuleController.SynchronizeModule(Dnn.Module.ModuleID);
    // Get updated hashtable
    settings = moduleController.GetTabModuleSettings(tabModuleId);
    
    // Let's set variables for ShowBadge function
    int imageCount = int.Parse((string)settings["ImageCount"]);
    Flickr.DisplayType displayType = (Flickr.DisplayType)Enum.Parse(typeof(Flickr.DisplayType), (string)settings["Display"]);    
    Flickr.SourceType source = (Flickr.SourceType)Enum.Parse(typeof(Flickr.SourceType), (string)settings["Source"]);
    string flickrId = (string)settings["FlickrId"];
    string tag = (string)settings["Tag"];    
    
    string relativePath = (HttpContext.Current.Request.ApplicationPath != "/") ? HttpContext.Current.Request.ApplicationPath : "";
}



@*Css with styles for Flickr badge*@
<link rel="stylesheet" type="text/css" href="@relativePath/DesktopModules/RazorModules/FlickrBadge/flickrbadge.css" />

@*Script for previewing images on current page. We want to add it only if OpenInFlickr is false *@
@if ((string)settings["OpenInFlickr"] != "1")
{            
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".badge-gallery").after("<div class='badge-display'></div>");
        $(".badge-gallery a").attr("href", "#");

        $(".badge-gallery img").each(function (index) {
            badgeThumb = $(this).attr("src");
            badgeImage = badgeThumb.replace("_s.jpg", "_z.jpg"); //str_replace("_s.jpg", "_z.jpg", badgeThumb);
            $(this).after("<img src='" + badgeImage + "' class='badge-preload' />");


        });

        $(".badge-gallery img").click(function () {
            $(".badge-gallery img").removeClass("badge-select");
            $(this).addClass("badge-select");
            badgeThumb = $(this).attr("src");
            badgeImage = badgeThumb.replace("_s.jpg", "_z.jpg");
            $(".badge-display img").remove();
            $(".badge-display").append("<img src='" + badgeImage + "' />");
            return false;
        });

        $(".badge-gallery img").first().click();
    });
</script>
}


<div class="badge-gallery">
@*I allow only square in this module simply because it looks the best. In general all other options are available.*@
@Flickr.ShowBadge(imageCount, displayType, Flickr.SizeType.Square, source, flickrId, tag)
</div>